#include "global.h"
#include "hBitStr.h"

hBitStr& hBitStr::operator<<(uint64_t ul)
{
	std::vector<uint8_t> tmp;
	do
	{
		tmp.push_back(ul & 1);
		ul >>= 1;
	} while (ul);

	while (tmp.size())
	{
		size_t dtId = _end >> dtBitOffset;
		if (dtId >= _data.size())
			_data.push_back(0);
		auto& chArr = _data[dtId];
		size_t chId = _end & dtBitMask;
		auto& bit = ((_hBitStrDetail::Bit8*)&chArr)[chId >> 3];
		bit[_end++ & 7] = tmp.back();
		tmp.pop_back();
	}

	return *this;
}

hBitStr& hBitStr::operator>>(bool& b)
{
	std::bitset<1> s;
	operator>>(s);
	b = s[0];
	return *this;
}

std::ostream& hBitStr::fillOs(std::ostream& os) const
{
	size_t n = 0;
	bool first = true;
	for (auto chArr : _data)
	{
		for (size_t i = first ? _beg : 0; i < dtBitSize && n + _beg < _end; ++i)
		{
			if (first) first = false;
			else if ((n & 3) == 0) os << " ";

			++n;
			auto& ch = ((char*)&chArr)[i >> 3];
			os << ((ch >> (7 - i & 7)) & 1);
		}
	}

	return os;
}